<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="服务商id" prop="agentId">
        <el-input
            v-model="queryParams.agentId"
            placeholder="请输入服务商id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="划拨批次号" prop="batchNumber">
        <el-input
            v-model="queryParams.batchNumber"
            placeholder="请输入划拨批次号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接收服务商或业务员" prop="personnelId">
        <el-input
            v-model="queryParams.personnelId"
            placeholder="请输入接收服务商或业务员"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始号段" prop="startSegment">
        <el-input
            v-model="queryParams.startSegment"
            placeholder="请输入起始号段"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束号段" prop="endSegment">
        <el-input
            v-model="queryParams.endSegment"
            placeholder="请输入结束号段"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请数" prop="applicationCount">
        <el-input
            v-model="queryParams.applicationCount"
            placeholder="请输入申请数"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成功数" prop="successCount">
        <el-input
            v-model="queryParams.successCount"
            placeholder="请输入成功数"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['pcdn:record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['pcdn:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['pcdn:record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['pcdn:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务商id" align="center" prop="agentId" v-if="columns[0].visible"/>
      <el-table-column label="划拨批次号" align="center" prop="batchNumber" v-if="columns[1].visible"/>
      <el-table-column label="接收服务商或业务员" align="center" prop="personnelId" v-if="columns[2].visible"/>
      <el-table-column label="起始号段" align="center" prop="startSegment" v-if="columns[3].visible"/>
      <el-table-column label="结束号段" align="center" prop="endSegment" v-if="columns[4].visible"/>
      <el-table-column label="申请数" align="center" prop="applicationCount" v-if="columns[5].visible"/>
      <el-table-column label="成功数" align="center" prop="successCount" v-if="columns[6].visible"/>
      <el-table-column label="调拨状态" align="center" prop="dialingStatus" v-if="columns[7].visible"/>
      <el-table-column label="人员类型" align="center" prop="personnelType" v-if="columns[8].visible"/>
      <el-table-column label="调拨类型" align="center" prop="type" v-if="columns[9].visible"/>
      <el-table-column label="设备编号集合,分割" align="center" prop="devices" v-if="columns[10].visible"/>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[11].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['pcdn:record:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['pcdn:record:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改调拨记录对话框 -->
    <el-dialog :title="title" v-model="open" :width="dialogWidth" append-to-body>
      <el-form ref="recordRef" :model="form" :rules="rules">
        <el-row :gutter="20">

          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="服务商id" prop="agentId">
              <el-input v-model="form.agentId" placeholder="请输入服务商id" />
            </el-form-item>
          </el-col>

          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="划拨批次号" prop="batchNumber">
              <el-input v-model="form.batchNumber" placeholder="请输入划拨批次号" />
            </el-form-item>
          </el-col>

          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="接收服务商或业务员" prop="personnelId">
              <el-input v-model="form.personnelId" placeholder="请输入接收服务商或业务员" />
            </el-form-item>
          </el-col>

          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="起始号段" prop="startSegment">
              <el-input v-model="form.startSegment" placeholder="请输入起始号段" />
            </el-form-item>
          </el-col>

          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="结束号段" prop="endSegment">
              <el-input v-model="form.endSegment" placeholder="请输入结束号段" />
            </el-form-item>
          </el-col>

          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="申请数" prop="applicationCount">
              <el-input v-model="form.applicationCount" placeholder="请输入申请数" />
            </el-form-item>
          </el-col>

          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="成功数" prop="successCount">
              <el-input v-model="form.successCount" placeholder="请输入成功数" />
            </el-form-item>
          </el-col>




          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="设备编号集合,分割" prop="devices">
              <el-input v-model="form.devices" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

          <el-col :span="isTwoColumnForm ? 12 : 24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Record">
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/pcdn/record"

const { proxy } = getCurrentInstance()

const recordList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

// 列显隐信息
const columns = ref([
  { key: 0, label: `服务商id`, visible: true },
  { key: 1, label: `划拨批次号`, visible: true },
  { key: 2, label: `接收服务商或业务员`, visible: true },
  { key: 3, label: `起始号段`, visible: true },
  { key: 4, label: `结束号段`, visible: true },
  { key: 5, label: `申请数`, visible: true },
  { key: 6, label: `成功数`, visible: true },
  { key: 7, label: `调拨状态`, visible: true },
  { key: 8, label: `人员类型（0 服务商 1 业务员）`, visible: true },
  { key: 9, label: `调拨类型（0 划拨 1 回拨）`, visible: true },
  { key: 10, label: `设备编号集合,分割`, visible: true },
  { key: 11, label: `备注`, visible: true },
])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    agentId: null,
    batchNumber: null,
    personnelId: null,
    startSegment: null,
    endSegment: null,
    applicationCount: null,
    successCount: null,
    dialingStatus: null,
    personnelType: null,
    type: null,
    devices: null,
  },
  rules: {
    batchNumber: [
      { required: true, message: "划拨批次号不能为空", trigger: "blur" }
    ],
    personnelId: [
      { required: true, message: "接收服务商或业务员不能为空", trigger: "blur" }
    ],
    startSegment: [
      { required: true, message: "起始号段不能为空", trigger: "blur" }
    ],
    endSegment: [
      { required: true, message: "结束号段不能为空", trigger: "blur" }
    ],
    applicationCount: [
      { required: true, message: "申请数不能为空", trigger: "blur" }
    ],
    successCount: [
      { required: true, message: "成功数不能为空", trigger: "blur" }
    ],
    dialingStatus: [
      { required: true, message: "调拨状态不能为空", trigger: "change" }
    ],
    personnelType: [
      { required: true, message: "人员类型不能为空", trigger: "change" }
    ],
    type: [
      { required: true, message: "调拨类型不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询调拨记录列表 */
function getList() {
  loading.value = true
  listRecord(queryParams.value).then(response => {
    recordList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    agentId: null,
    batchNumber: null,
    personnelId: null,
    startSegment: null,
    endSegment: null,
    applicationCount: null,
    successCount: null,
    dialingStatus: null,
    personnelType: null,
    type: null,
    devices: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("recordRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加调拨记录"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRecord(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改调拨记录"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["recordRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除调拨记录编号为"' + _ids + '"的数据项？').then(function() {
    return delRecord(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('pcdn/record/export', {
    ...queryParams.value
  }, `record_${new Date().getTime()}.xlsx`)
}

getList()

/** 动态计算字段数 **/
const fieldCount = ref(0)

onMounted(() => {
  // 统计表单字段数量
  fieldCount.value = [
    "agentId",
    "batchNumber",
    "personnelId",
    "startSegment",
    "endSegment",
    "applicationCount",
    "successCount",
    "dialingStatus",
    "personnelType",
    "type",
    "devices",
    "remark",
  ].length
})

const isTwoColumnForm = computed(() => fieldCount.value > 8)
const dialogWidth = computed(() => isTwoColumnForm.value ? '60vw' : '500px')

</script>
